package org.dromara.pe.controller;

import java.time.Duration;
import java.util.Date;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.config.RedisConfig;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.run.domain.bo.RuleBo;
import org.dromara.run.domain.vo.RuleVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.pe.domain.vo.TaskInfoVo;
import org.dromara.pe.domain.bo.TaskInfoBo;
import org.dromara.pe.service.ITaskInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 体测任务管理
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pe/taskInfo")
public class TaskInfoController extends BaseController {

    private final ITaskInfoService taskInfoService;

    /**
     * 查询体测任务管理列表
     */
    @SaCheckPermission("pe:taskInfo:list")
    @GetMapping("/list")
    public TableDataInfo<TaskInfoVo> list(TaskInfoBo bo, PageQuery pageQuery) {
        return taskInfoService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("pe:taskInfo:query")
    @GetMapping("/infoList")
    public R<List<TaskInfoVo>> getInfoList(TaskInfoBo bo) {
        return R.ok(taskInfoService.queryList(bo));
    }

    /**
     * 导出体测任务管理列表
     */
    @SaCheckPermission("pe:taskInfo:export")
    @Log(title = "体测任务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TaskInfoBo bo, HttpServletResponse response) {
        List<TaskInfoVo> list = taskInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "体测任务管理", TaskInfoVo.class, response);
    }

    /**
     * 获取体测任务管理详细信息
     *
     * @param taskId 主键
     */
    @SaCheckPermission("pe:taskInfo:query")
    @GetMapping("/{taskId}")
    public R<TaskInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long taskId) {
        return R.ok(taskInfoService.queryById(taskId));
    }

    /**
     * 新增体测任务管理
     */
    @SaCheckPermission("pe:taskInfo:add")
    @Log(title = "体测任务管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TaskInfoBo bo) {
        return toAjax(taskInfoService.insertByBo(bo));
    }

    /**
     * 修改体测任务管理
     */
    @SaCheckPermission("pe:taskInfo:edit")
    @Log(title = "体测任务管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaskInfoBo bo) {
        return toAjax(taskInfoService.updateByBo(bo));
    }
    /**
     * 手动结束任务
     */
    @SaCheckPermission("pe:taskInfo:edit")
    @Log(title = "手动结束任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/manually/end/task")
    public R<Void> manuallyEndTask(@Validated(EditGroup.class) @RequestBody TaskInfoBo bo) {
        bo.setTaskStatus(2L);
        Boolean b = taskInfoService.updateByBo(bo);
//        if(b){
//            String key="taskInfo|"+bo.getTaskId().toString();
//            RedisUtils.deleteKeys(key);
//        }
        return toAjax(b);
    }
    @SaCheckPermission("pe:taskInfo:edit")
    @Log(title = "手动恢复任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/manually/recover/task")
    public R<Void> manuallyRecoverTask(@Validated(EditGroup.class) @RequestBody TaskInfoBo bo) {
        bo.setTaskStatus(1L);
        Boolean b = taskInfoService.updateByBo(bo);
        if(b){
            Date date=new Date();
            bo.setTaskId(bo.getTaskId());
            long times = bo.getEndDate().getTime() - date.getTime();
            String key="taskInfo|"+bo.getTaskId().toString();
            RedisUtils.deleteKeys(key);
            RedisUtils.setCacheObject(key, "value", Duration.ofMinutes(times/1000));
        }
        return toAjax(b);
    }

    /**
     * 删除体测任务管理
     *
     * @param taskIds 主键串
     */
    @SaCheckPermission("pe:taskInfo:remove")
    @Log(title = "体测任务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] taskIds) {
        return toAjax(taskInfoService.deleteWithValidByIds(List.of(taskIds), true));
    }

}
